/**
 * 
 */
package com.wixc.util;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
 * @author weichuanxi
 * @date 2016年4月26日
 */
public class ImageUtil {
	private static Logger log = Logger.getLogger(ImageUtil.class);

	/**
	 * 下载文件到本地
	 * @param urlString
	 *            被下载的文件地址
	 * @param filename
	 *            本地文件名
	 * @throws Exception
	 *             各种异常
	 */
	public static void download(String urlString, String filename) throws Exception {
		URL url = new URL(urlString);
		URLConnection con = url.openConnection();
		InputStream is = con.getInputStream();
		byte[] bs = new byte[1024];
		int len;
		OutputStream os = new FileOutputStream(filename);
		while ((len = is.read(bs)) != -1) {
			os.write(bs, 0, len);
		}
		os.close();
		is.close();
	}
	
	
	public static void resizeImage(InputStream is, OutputStream os,int width,int height) throws IOException {  
	    BufferedImage prevImage = ImageIO.read(is);  
	    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);  
	    Graphics graphics = image.createGraphics();  
	    graphics.drawImage(prevImage, 0, 0, width, height, null);  
	    ImageIO.write(image, "png", os);  
	    os.flush();  
	    is.close();  
	    os.close();  
	}  
	
	
	
	 public static void delFolder(String folderPath) {
	     try {
	        delAllFile(folderPath); //删除完里面所有内容
	        String filePath = folderPath;
	        filePath = filePath.toString();
	        java.io.File myFilePath = new java.io.File(filePath);
	        myFilePath.delete(); //删除空文件夹
	     } catch (Exception e) {
	       e.printStackTrace(); 
	     }
	}

	//删除指定文件夹下所有文件
	//param path 文件夹完整绝对路径
	   public static boolean delAllFile(String path) {
	       boolean flag = false;
	       File file = new File(path);
	       if (!file.exists()) {
	         return flag;
	       }
	       if (!file.isDirectory()) {
	         return flag;
	       }
	       String[] tempList = file.list();
	       File temp = null;
	       for (int i = 0; i < tempList.length; i++) {
	          if (path.endsWith(File.separator)) {
	             temp = new File(path + tempList[i]);
	          } else {
	              temp = new File(path + File.separator + tempList[i]);
	          }
	          if (temp.isFile()) {
	             temp.delete();
	          }
	          if (temp.isDirectory()) {
	             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
	             delFolder(path + "/" + tempList[i]);//再删除空文件夹
	             flag = true;
	          }
	       }
	       return flag;
	     }
	   
	   //文件夹是否为空
	   public static boolean hasFile(String path) {
		   boolean flag = false;
	       File file = new File(path);
	       if (!file.exists()) {
	         return flag;
	       }
	       if (!file.isDirectory()) {
		         return flag;
		       }
	       
	       String[] tempList = file.list();
	       if (tempList.length>0) {
	    	   log.info("not empty");
	    	   flag=false;
	       }else{
	    	   flag=true;
	    	   log.info("is empty");
	    	   
	       }
	       
		   return flag;
	   }
	   
	   public static  String flienames(String path){
		    File file = new File(path);
		    String[] tempList = file.list();
		    if (tempList.length>0) {
		    	  return tempList[0];
			}else{
				return "";
			}
	   }
	   
	   //下载到imgcode文件夹中
	   public static String downloadImg(String url){
			String img=UUID.randomUUID()+MyStringUtils.DateToStr(new Date(), "yyyy-MM-dd")+".png";
			String target=ConstantsUtil.path+ConstantsUtil.split+"imgcode"+ConstantsUtil.split+img;
			log.info("target>>>"+target);
			String absolutpath="images"+ConstantsUtil.split+img;
			try {
				ImageUtil.download(url, target);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return absolutpath;
	   }
	   
	   public static void main(String[] args) throws Exception {
		   //清空imgcode文件夹
		   //delAllFile(System.getProperty("user.dir")+File.separator+"imgcode");
		   System.out.println(flienames(ConstantsUtil.path+ConstantsUtil.split+"imgcode").contains("2016-04-29"));
		}
}
